//foodController.$inject = ['foodService','$modal'];
var $ = require("jquery");

const _ = require('lodash');

function foodController(foodService, $uibModal, $scope, $window) {

    var vm = this;
    vm.foods = [];
    vm.foods_bak = [];
    vm.searchInput = '';

    foodService.getAllFoods().then(function (data) {
        vm.foods = data.data.data;
        vm.foods_bak = data.data.data;
    });

    vm.add = function () {

        $('#inputName').val("");
        $('#inputPrice').val("");
        $('#inputImg').val("");

        $('#addFoodModal').modal({backdrop: 'static', keyboard: false});
        $("#addFoodModal").modal('show');
    }

    vm.addNFood = function () {

        var form = document.getElementById("addForm");
        var fd = new FormData(form);

        if(fd.get('name')==''||fd.get('price')==''){
            swal({
                title : "Please input again",
                text : 'not allow empty field',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('name').length > 30){
            swal({
                title : "Please input again",
                text : 'not allow length of name more than 30',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('img').name==''){
            swal({
                title : "Please select again",
                text : 'not choose your file',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }
        foodService.addFood(fd).then(function (data) {

            if (data.data.code == 200) {

                vm.foods.push(data.data.data);
                $("#addFoodModal").modal('hide');
                $(".modal-backdrop").remove();

            } else {
                alert('Oops something went wrong!');
            }
        });


    }

    vm.modify = function ($Index, tmp) {
        vm.updatetmp = tmp;
        $('#updateFoodModal').modal({backdrop: 'static', keyboard: false});
        $("#updateFoodModal").modal('show');
    }


    vm.resetUpdateFood = function () {
        $("#updateFoodModal").modal('hide');
        $(".modal-backdrop").remove();
    }


    vm.modifyFood = function () {

        var form = document.getElementById("updateForm");
        var fd = new FormData(form);
        if(fd.get('name')==''||fd.get('price')==''){
            swal({
                title : "Please input again",
                text : 'not allow empty field',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('name').length > 30){
            swal({
                title : "Please input again",
                text : 'not allow length of name more than 30',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('img').name==''){
            swal({
                title : "Please select again",
                text : 'not choose your file',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }
        foodService.modifyFoodByFoodId(fd).then(function (data) {

            console.log(data.data);
            if (data.data.code == 200) {

                removeById(vm.foods_bak,data.data.data.food_id);
                removeById(vm.foods,data.data.data.food_id);

                vm.foods.push(data.data.data);
                // vm.foods_bak.push(data.data.data);

                $("#updateFoodModal").modal('hide');
                $(".modal-backdrop").remove();
            } else {
                alert('Oops something went wrong!');
            }
        });
    }

    vm.delete = function ($Index, tmp) {
        vm.deletetmp = tmp;
        $('#deleteFoodModal').modal({backdrop: 'static', keyboard: false});
        $("#deleteFoodModal").modal('show');
    }

    vm.resetDeleteFood = function () {
        $("#deleteFoodModal").modal('hide');
        $(".modal-backdrop").remove();
    }

    vm.deleteFood = function (food_id) {

        foodService.delFoodByFoodId(food_id).then(function (data) {

            if (data.data.code == 200) {

                removeById(vm.foods_bak,food_id);
                removeById(vm.foods,food_id);

                $("#deleteFoodModal").modal('hide');
                $(".modal-backdrop").remove();

            } else {

                alert('Oops something went wrong!');
            }
        });

    }


    vm.search = function () {
        if (!_.isEmpty(vm.searchInput) && vm.foods.length > 0) {
            vm.foods = [];
            for (var index in vm.foods_bak) {
                if (vm.foods_bak[index].name.indexOf(vm.searchInput) > -1) {
                    vm.foods.push(vm.foods_bak[index]);
                }
            }
        }
        else {
            vm.foods = vm.foods_bak;
        }
    };


    function removeById(arr, id) {
        for(var i=0; i<arr.length; i++) {
            if(arr[i].food_id === id) {
                arr.splice(i, 1);
                break;
            }
        }
    }

}

module.exports = foodController;

